<template>
  <div>
    <!-- excel -->
    <el-card align="right" style="margin-top: 15px" class="top-card">
      <el-button type="primary">excel 导入</el-button>
      <el-button type="success">excel 导出</el-button>
    </el-card>

    <!-- 表格 -->
    <el-card style="margin-top: 25px" class="table-card">
      <el-table :data="userList" border style="width: 100%">
        <el-table-column type="index" label="#"> </el-table-column>
        <!-- <el-table-column
          v-for="(item, index) in tableData"
          :key="index"
          :prop="item.prop"
          :label="item.label"
        >
        </el-table-column> -->
        <el-table-column prop="username" label="员工姓名"></el-table-column>
        <el-table-column prop="mobile" label="联系方式"></el-table-column>
        <el-table-column prop="avatar" label="头像" align="center">
          <template slot-scope="scope">
            <el-image class="table-img" :preview-src-list="[scope.row.avatar]" :src="scope.row.avatar" alt=""></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="role" label="角色">
          <template slot-scope="scope">
            <el-tag size="mini" v-for="item in scope.row.role" :key="item.id">
              {{ item.title }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="openTime" label="开通时间">
          <template slot-scope="scope">
            {{ parseInt(scope.row.openTime) | getTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleClick(scope.row._id)"
              >查看</el-button
            >
            <el-button size="mini" type="info" @click="dialogVisible = true"
              >角色</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="delClick(scope.row._id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        align="center"
        style="margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-dialog
      title="配置角色"
      :visible.sync="dialogVisible"
      width="700px"
    >
      <el-checkbox-group v-model="checkList">
        <el-checkbox label="复选框 A"></el-checkbox>
        <el-checkbox label="复选框 B"></el-checkbox>
        <el-checkbox label="复选框 C"></el-checkbox>
      </el-checkbox-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import moment from "moment";
import userApi from "../../api/sys";
export default {
  data() {
    return {
      page: 1,
      size: 2,
      total: 0,
      userList: [],
      dialogVisible: false,
      checkList: [],
    };
  },
  mounted() {
    this.getUserList();
  },
  computed: {},
  filters: {
    getTime(date) {
      return moment(date).format("YYYY-MM-DD");
    },
  },
  methods: {
    async getUserList() {
      let res = await userApi.getUserList(this.page, this.size);
      console.log(res);
      this.userList = res.list;
      this.total = res.total;
    },
    handleSizeChange(val) {
      this.size = val;
      this.getUserList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getUserList();
    },
    handleClick(id) {
      this.$router.push(`/user/info/${id}`);
    },
    // 删除
    async delClick(id) {
      let res = await userApi.delUser(id);
      console.log(res);
      this.page = 1;
      this.getUserList();
    },
  },
};
</script>

<style scoped lang='scss'>
.table-img {
  width: 60px;
  height: 60px;
  border-radius: 30px;
}
</style>
